package com.kermel.andromeda.clock.view;

import java.text.DateFormat;
import java.util.Calendar;

import android.content.Context;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.kermel.andromeda.clock.format.CurrentTimeFormatter;
import com.kermel.andromeda.data.settings.ClockSettings;
import com.kermel.andromeda.data.settings.ClockSettingsPersister;
import com.kermel.andromeda.data.settings.SettingsManager;
import com.kermel.andromeda.engine.CurrentTimeSubscriber;
import com.kermel.andromeda.engine.CurrentTimeTicker;
import com.kermel.andromeda.engine.view.CurrentTimeViewUpdater;

@SuppressWarnings("unused")
public class ClockPM implements CurrentTimeSubscriber {
    private static final int FULL_DATE_DISPLAY_ID = 0;
    private static final int SHORT_DATE_DISPLAY_ID = 1;
    
    private CurrentTimeTicker timeTicker;
    private CurrentTimeViewUpdater currentTimeViewUpdater;
    private ClockSettings clockSettings;
    private ClockSettingsPersister clockSettingsPersister;
    private CurrentTimeFormatter currentTimeFormatter;
    
    public ClockPM(Context context) {
        initCurrentTimeTicker(this);
        initClockSettingsPersister(context);
        initClockSettings();
        initCurrentTimeFormatter();
    }
    
    private void initCurrentTimeTicker(CurrentTimeSubscriber currentTimeSubscriber) {
        timeTicker = new CurrentTimeTicker(currentTimeSubscriber);
    }
    
    private void initClockSettingsPersister(Context context) {
        clockSettingsPersister = new ClockSettingsPersister(context);
    }
    
    private void initClockSettings() {
        clockSettings = SettingsManager.instance.getClockSettings();
        if (clockSettingsPersister.retrieve(clockSettings) == false) {
            // TODO: Report to the user there was an error and that default values are being used.
        }
    }
    
    private void initCurrentTimeFormatter() {
        currentTimeFormatter = new CurrentTimeFormatter(clockSettings);
    }
    
    public void registerTimeView(TextView timeView) {
        initCurrentTimeViewUpdater(timeView);
    }
    
    private void initCurrentTimeViewUpdater(TextView timeView) {
        currentTimeViewUpdater = new CurrentTimeViewUpdater(timeView, currentTimeFormatter);
    }
    
    public CharSequence getCurrentTimeAsString() {
        long currentTime = System.currentTimeMillis();
        return currentTimeViewUpdater.formatCurrentTimeAsString(currentTime);
    }
    
    public void onCurrentTimeTick(long currentTime) {
        currentTimeViewUpdater.updateCurrentTime(currentTime);
    }
    
    ClockSettings getClockSettings() {
        return clockSettings;
    }
    
    void setClockSettings(ClockSettings clockSettings) {
        this.clockSettings = clockSettings;
    }
    
    public boolean isDateDisplayed() {
        return clockSettings.isDisplayDate();
    }
    
    public boolean isFullDateDisplayed() {
        boolean result = false;
        
        if (isDateDisplayed())
            result = clockSettings.getDateDisplayType().isFull();
        
        return result;
    }
    
    public boolean isShortDateDisplayed() {
        boolean result = false;
        
        if (isDateDisplayed())
            result = clockSettings.getDateDisplayType().isShort();
        
        return result;
    }
    
    public void switchTypeOfDateDisplayBasedOnSettings(ViewSwitcher dateViewSwitcher) {
        if (dateViewSwitcher != null) {
            if (isFullDateDisplayed()) {
                dateViewSwitcher.setDisplayedChild(FULL_DATE_DISPLAY_ID);
            } 
            else if (isShortDateDisplayed()) {
                dateViewSwitcher.setDisplayedChild(SHORT_DATE_DISPLAY_ID);
            }
        }
    }
    
    public String buildFullDateString() {
        Calendar today = Calendar.getInstance();
        DateFormat formatter = DateFormat.getDateInstance(DateFormat.FULL);
        String dateString = formatter.format(today.getTime());
        return dateString;
    }
    
    public boolean isMilitaryTime() {
        return clockSettings.isMilitaryTime();
    }
}
